<?php
require_once ('config.php');

$action = $_GET['action'];
//echo ('action:' . $action . '<br>');

if ($action == "reg") {
	$userid = $_POST['id'];
	$gcmid = $_POST['gcmid'];
	$password = $_POST['pw'];
	register($userid, $password, $gcmid);
}

function register($userid, $password, $gcmid) {
	$dsn = "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME;
	$db = new PDO($dsn, DB_USER, DB_PASSWORD);

	$rs = $db->prepare("insert into " . TAB_USER . "(userid,gcmid,password) values(:U,:G,:P)");
	$rs->bindValue(':U', $userid);
	$rs->bindValue(':P', $password);
	$rs->bindValue(':G', $gcmid);
	$count = $rs->execute();

	if ($db->errorCode() != '00000') {
		//		print_r('ERROR:' . $db->errorInfo());
		$result = array (
			"result" => "error",
			"cause" => "db_error",
			"info" => $db->errorInfo()
		);
		die(json_encode($result));
	} else {
		if ($count <= 0) {
			$result = array (
				"result" => "error",
				"cause" => "db_error",
				"info" => "insert 0 row.",
				"userid" => $userid,
				"gcmid" => $gcmid,
				"password" => $password
			);
			die(json_encode($result));
		} else {
			$result = array (
				"result" => "ok"
			);
			echo (json_encode($result));
		}
	}
	$db = null;
}
?>
